package com.cluster.platform.controller;

import com.cluster.platform.common.api.CommonResult;
import com.cluster.platform.dto.SysConfigDto;
import com.cluster.platform.model.SysConfig;
import com.cluster.platform.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**系统配置controller
 * @ClassName: SysConfigController
 */
@Controller
@Api(tags = "SysConfigController", value = "系统配置")
@RequestMapping("/sysConfig")
public class SysConfigController {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Value("${aliyun.oss.dir.prefix}")
    private String prefix;

    @Value("${aliyun.oss.maxSize}")
    private String maxSize;

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.callback}")
    private String callback;

    @Value("${focus.ceiling}")
    private String ceiling;

    @ApiOperation("获取oss云对象脸接参数")
    @RequestMapping(value = "/ossConfig", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Object> ossUrl() {
        Map<String,String> ossConfig = new HashMap<>();
        ossConfig.put("endpoint",endpoint);
        ossConfig.put("bucketName",bucketName);
        ossConfig.put("prefix",prefix);
        ossConfig.put("maxSize",maxSize);
        ossConfig.put("accessKeyId",accessKeyId);
        ossConfig.put("accessKeySecret",accessKeySecret);
        ossConfig.put("callback",callback);
        if (ossConfig.isEmpty()) {
            return CommonResult.failed("未找到参数信息");
        }
        return CommonResult.success(ossConfig);
    }

    @ApiOperation("获取关注上限参数")
    @RequestMapping(value = "/maxCeiling", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Object> maxCeiling() {
        if (StringUtils.isBlank(ceiling)) {
            return CommonResult.failed("未找到参数信息");
        }
        return CommonResult.success(ceiling);
    }

    @Autowired
    SysConfigService sysConfigService;

    @ApiOperation("批量操作")
    @RequestMapping(value = "/batchSysConfig", method = RequestMethod.PUT)
    @ResponseBody
    public CommonResult<Object> batchSysConfig(@RequestParam("status") Integer status, @RequestParam("ids")List<String> ids) {
        return sysConfigService.batchSysConfig(status,ids);
    }

    @ApiOperation("获取系统配置信息--分页")
    @RequestMapping(value = "/searchSysConfigPage", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Object> searchSysConfigPage(@RequestBody SysConfigDto sysConfigDto) {
        return sysConfigService.fetchList(sysConfigDto);
    }

    @ApiOperation("获取系统配置信息")
    @RequestMapping(value = "/searchSysConfig", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Object> searchSysConfig(@RequestBody SysConfig sysConfig) {
        return sysConfigService.searchSysConfig(sysConfig);
    }

    @ApiOperation("新增系统配置信息")
    @RequestMapping(value = "/addSysConfig", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Object> addSysConfig(@RequestBody SysConfig sysConfig) {
        return sysConfigService.addSysConfig(sysConfig);
    }

    @ApiOperation("修改系统配置信息")
    @RequestMapping(value = "/updateSysConfig", method = RequestMethod.PUT)
    @ResponseBody
    public CommonResult<Object> updateSysConfig(@RequestBody SysConfig sysConfig) {
        return sysConfigService.updateSysConfig(sysConfig);
    }

    @ApiOperation("删除系统配置信息")
    @RequestMapping(value = "/delSysConfig", method = RequestMethod.DELETE)
    @ResponseBody
    public CommonResult<Object> delSysConfig(@RequestBody SysConfig sysConfig) {
        return sysConfigService.delSysConfig(sysConfig);
    }
}
